﻿using System;

namespace DotNetCore.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            //Console.WriteLine("**************");
            //Console.WriteLine("*   Start!   *");
            //Console.WriteLine("**************");
            var biTree = new BiTree
            {
                Root = "A",
                LeftChildBiTree = new BiTree {Root = "B", RightChildBiTree = new BiTree {Root = "C", LeftChildBiTree = new BiTree {Root = "D"}}},
                RightChildBiTree = new BiTree
                {
                    Root = "E",
                    RightChildBiTree = new BiTree
                    {
                        Root = "F",
                        LeftChildBiTree = new BiTree {Root = "G", LeftChildBiTree = new BiTree {Root = "H"}, RightChildBiTree = new BiTree {Root = "K"}}
                    }
                }
            };

            //前序遍历
            Console.WriteLine("前序遍历");
            PreOrderTraverse(biTree);
            Console.WriteLine(" ");
            Console.WriteLine(" ");

            //中序遍历
            Console.WriteLine("中序遍历");
            InOrderTraverse(biTree);
            Console.WriteLine(" ");
            Console.WriteLine(" ");

            //后序遍历
            Console.WriteLine("前序遍历");
            PostOrderTraverse(biTree);

            Console.ReadKey();
        }

        /// <summary>
        /// 前序遍历
        /// </summary>
        private static void PreOrderTraverse(BiTree biTree)
        {
            if (biTree!=null)
            {
                //访问根结点
                Console.Write(biTree.Root);
                Console.Write(",");
                //前序遍历左子树
                PreOrderTraverse(biTree.LeftChildBiTree);
                //前序遍历右子树
                PreOrderTraverse(biTree.RightChildBiTree); 
            }
        }

        /// <summary>
        /// 中序遍历
        /// </summary>
        private static void InOrderTraverse(BiTree biTree)
        {
            if (biTree != null)
            {
                //中序遍历左子树
                InOrderTraverse(biTree.LeftChildBiTree);        
                //访问根结点
                Console.Write(biTree.Root);
                Console.Write(",");
                //中序遍历右子树
                InOrderTraverse(biTree.RightChildBiTree);        
            }
        }

        /// <summary>
        /// 后序遍历
        /// </summary>
        private static void PostOrderTraverse(BiTree biTree)
        {
            if (biTree != null)
            {
                //后序遍历左子树
                PostOrderTraverse(biTree.LeftChildBiTree);

                //后序遍历右子树
                PostOrderTraverse(biTree.RightChildBiTree);
                //访问根结点
                Console.Write(biTree.Root);
                Console.Write(",");
            }
        }
    }

    class BiTree
    {
        public string Root { get; set; }

        public BiTree LeftChildBiTree { get; set; }

        public BiTree RightChildBiTree { get; set; }
    }
}
